/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ServicesAccessData;

import DAO.Memcached;
import DAO.StatusDAO;
import DAO.TagDAO;
import DAO.UserDAO;
import POJO.StatusPOJO;
import POJO.TagNameAndCounter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import profiler.*;

/**
 *
 * @author huy
 */
public class UserImplProfiled implements User.Iface {

    private TTransport tr;
    private Cassandra.Client client;

    public UserImplProfiled() throws InvalidRequestException, UnavailableException {
        try {
            tr = new TFramedTransport(new TSocket(Common.Constants.HOST, Common.Constants.PORT));
            TProtocol proto = new TBinaryProtocol(tr);
            client = new Cassandra.Client(proto);
            tr.open();
            client.set_keyspace(Common.Constants.KEYSPACE);

            //Khởi tạo cache.
            List<String> listMem = new ArrayList<String>();
            (StatusDAO.getInstance(client)).createCache(listMem);
            (TagDAO.getInstance(client)).createCacheTag(listMem);
            (UserDAO.getInstance(client)).createCache(listMem);
            Memcached.set("ListMemId", Common.Constants.EXPIRE, listMem);

        } catch (TException ex) {
            Logger.getLogger(UserImplProfiled.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public boolean likeStatus(String statusID) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Like status");
        StatusDAO status = StatusDAO.getInstance(client);
        boolean result = status.likeStatus(statusID);
        ProfilerLog.getInstance("User").doEndLog("Like status");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean dislikeStatus(String statusID) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Dislike status");
        StatusDAO status = StatusDAO.getInstance(client);
        boolean result = status.dislikeStatus(statusID);
        ProfilerLog.getInstance("User").doEndLog("Dislike status");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public List<StatusPOJO> selectStatusFromTag(String tagName) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select status from tag");
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectStatusFromTag(tagName);
        ProfilerLog.getInstance("User").doEndLog("Select status from tag");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public List<StatusPOJO> selectTopStatusFromTag(String tagName, int limit) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select top status from tag");
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectTopStatusFromTag(tagName, limit);
        ProfilerLog.getInstance("User").doEndLog("Select top status from tag");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public List<StatusPOJO> selectTopStatus(int limit) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select top status");
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectTopStatus(limit);
        ProfilerLog.getInstance("User").doEndLog("Select top status");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public List<StatusPOJO> selectRanDomStatus(int limit) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select random status");
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectRandomStatusV2(limit);
        ProfilerLog.getInstance("User").doEndLog("Select random status");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public List<StatusPOJO> selectRandomStatusFromTag(String tagName, int limit) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select random status from tag");
        StatusDAO status = StatusDAO.getInstance(client);
        List<StatusPOJO> result = status.selectRandomStatusFromTag(tagName, limit);
        ProfilerLog.getInstance("User").doEndLog("Select random status from tag");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public List<TagNameAndCounter> selectAllTagNameAndCounter() throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select all tag name and counter");
        TagDAO tag = TagDAO.getInstance(client);
        List<TagNameAndCounter> result = tag.selectAllTagNameAndCounter();
        ProfilerLog.getInstance("User").doEndLog("Select all tag name and counter");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public List<TagNameAndCounter> selectTagNameAndCounter(int limit) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select tag name and counter");
        TagDAO tag = TagDAO.getInstance(client);
        List<TagNameAndCounter> result = tag.selectTagNameAndCounter(limit);
        ProfilerLog.getInstance("User").doEndLog("Select tag name and counter");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean isBannedUser(String userID) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Is banned user");
        UserDAO user = UserDAO.getInstance(client);
        boolean result = user.isBannedUser(userID);
        ProfilerLog.getInstance("User").doEndLog("Is banned user");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public List<StatusPOJO> selectFavoriteStatus(String userID) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select favorite status");
        UserDAO user = UserDAO.getInstance(client);
        List<StatusPOJO> result = user.selectFavoriteStatus(userID);
        ProfilerLog.getInstance("User").doEndLog("Select favorite status");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean insertFavoriteStatus(String userID, String statusID) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Insert favorite status");
        UserDAO user = UserDAO.getInstance(client);
        boolean result = user.insertFavoriteStatus(userID, statusID);
        ProfilerLog.getInstance("User").doEndLog("Insert favorite status");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean removeFavoriteStatus(String userID, String statusID) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Remove favorite status");
        UserDAO user = UserDAO.getInstance(client);
        boolean result = user.removeFavoriteStatus(userID, statusID);
        ProfilerLog.getInstance("User").doEndLog("Remove favorite status");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean isAdminUser(String userID) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Is admin user");
        UserDAO user = UserDAO.getInstance(client);
        boolean result = user.isAdminUser(userID);
        ProfilerLog.getInstance("User").doEndLog("Is admin user");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean selectStatusFromTag_cache(String tagName) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select status from tag (CACHED)");
        boolean result = false;
        try {
            result = (StatusDAO.getInstance(client)).cachedStatusFromTag(tagName);
        } catch (UnavailableException ex) {
            Logger.getLogger(UserImplProfiled.class.getName()).log(Level.SEVERE, null, ex);
        }
        ProfilerLog.getInstance("User").doEndLog("Select status from tag (CACHED)");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean selectTopStatusFromTag_cache(String tagName) throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select top status from tag (CACHED)");
        boolean result = false;
        result = (StatusDAO.getInstance(client)).selectTopStatusFromTag_cache(tagName, 20);
        ProfilerLog.getInstance("User").doEndLog("Select top status from tag (CACHED)");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean selectTopStatus_cache() throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select top status (CACHED)");
        boolean result = false;
        result = (StatusDAO.getInstance(client)).selectTopStatus_cache(20);
        ProfilerLog.getInstance("User").doEndLog("Select top status (CACHED)");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean selectAllTagNameAndCounter_cache() throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select all tag name and counter (CACHED)");
        boolean result = false;
        result = (TagDAO.getInstance(client)).cachedAllTagNameAndCounter();
        ProfilerLog.getInstance("User").doEndLog("Select all tag name and counter (CACHED)");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }

    @Override
    public boolean selectAllStatus_cache() throws TException {
        ProfilerLog.getInstance("User").doStartLog("Select all status (CACHED)");
        boolean result = false;
        result = (StatusDAO.getInstance(client)).selectAllStatus_cache();
        ProfilerLog.getInstance("User").doEndLog("Select all status (CACHED)");
        System.out.print(ProfilerLog.getInstance("User").dumpLogConsole());
        return result;
    }
}
